<h1 class="right">تقرير نسبة الإشغال</h1>

<div class="clear"></div>

<div id="dashboard-container">
	<div class="customers-filter">
    	<form name="contract_search" action="" method="POST" id="searchClient">
      <table class="bookings-filter">
        	
          <tr>
		  <td style="width:15px;">من  </td>
                <td >
                  <input type="text" class="small-input" name="from" id="from-0" placeholder=""  value="<?php if(isset($binds['from']) ) {echo $binds['from'];} ?>"/>
                </td>
                <td>
                </td>
                <td>الى</td>
               <td><input type="text" class="small-input" name="to" id="to-0" placeholder="" value="<?php if(isset($binds['to']) ) {echo $binds['to'];} ?>" /></td>
                <td style="width:60px;">رقم الوحدة</td>
                <td style="width:70px; padding-left:20px;"><input type="text" class="xsmall-input" name="payment_name" value="<?php if(isset($binds['payment_name']) ) {echo $binds['payment_name'];} ?>"></td>
                <td style="width:60px;">رقم الطابق</td>
                <td style="width:70px; padding-left:20px;"><input type="text" class="xsmall-input" name="floor" value="<?php if(isset($binds['floor']) ) {echo $binds['floor'];} ?>"></td>
				<input type="hidden" id="page" name="page" value="" />
                
                 <td style="width:15px">
                  <input type="hidden" name="filteration" value="11"/> <!-- 11 for traffic Reposts -->
                  <a href="#"><div class="export-button">تصدير Excel</div></a>
                  
                  <input type="hidden" id="pdf_excel" name="pdf_excel" value="110"/>
                </td>
                <td style="width:15px"><a href="#"><div class="export-PDF">تصدير PDF</div></a></td>
                <td style="width:50px"><div class="search-botton">بحث</div></td> 
            </tr>
            
      </table>
      </form>
    </div>
    
    
    <div class="dashboard-row">
    
    	<table class="customers-table">
        <thead>
            <tr class="customers-table-head">
            	<td width="">رقم الوحدة</td>
              <td width="">نسبة الاشغال</td>
            </tr>
            </thead>
            <?php 
			$sum=0;
			
			if(isset($binds['to']) && isset($binds['from'])) {
			foreach($traffic as $traf) {
            
			?>
                <tr class="">
                    <td width=""><?php echo $traf['apart_name'] ; ?></td>
					<?php 
				
                    $total = (strtotime($binds['to'])-strtotime($binds['from']))/86400 ;
				$per = query("SELECT SUM(period) FROM contract_apartments LEFT JOIN 
				              contracts ON 
							  contract_apartments.contract_id=contracts.contract_id 
							  WHERE contract_apartments.apartment_id = ? and mildai_from between ? AND ?  ",$traf['id'],$binds['from'],$binds['to']);
							  
							  if(empty($per[0]['SUM(period)'])){
							   
							  echo "<td > %0 </td>" ;
            
							  }
							 else if($total <= $per[0]['SUM(period)']){
							   echo "<td > %100 </td>" ;
							  }
					
					else{ 
					
					$percentage = ($per[0]['SUM(period)'] /$total)*100 ;
					$sum = $sum+$percentage; 
					
					
					?>
                    <td class="" width=""><?php echo "%".round($percentage,2); ?></td>
                </tr>
            <?php   } } }
			
			 ?>
		 
        <hr />
        </table>
        <?php
		if(isset($binds['to']) && isset($binds['from'])) {
         $start = $page * 20 -19;
          $end  = $start + 19;
          if($end > $totalPages)
            $end = $totalPages
          
        ?>
	<div class="statistics">
     <?php 
	   if(isset($binds['payment_name'])){  
	 echo " نسبة اشغال الوحدة رقم ".$binds['payment_name']." خلال الفترة من ".$binds['from']. " الى ".$binds['to'] ." = ".round($sum/$totalPages , 2)."%";
	    }
	 else if(isset($binds['floor'])){
	 echo " متوسط نسبة إشغال الوحدات الموجوده بالطابق رقم ".$binds['floor']." خلال الفترة من ".$binds['from']. " الى ".$binds['to'] ." = ".round($sum/$totalPages,2)."%";
	 }
	  else {
	  $sum2=0;
	 
	  $S= ($tot[0]['SUM(period)'] /$totalPages) ;
	 
	  echo " متوسط نسبة اشغال الوحدات  فى الفندق كامل "." خلال الفترة من ".$binds['from']. " الى ".$binds['to'] ." = ".(round($S/$totalPages,2)*100)."%";
	 }
      ?>
    </div>
	<?php if(!empty($traffic)) { ?>
        <div class="clear"></div>
        <div class="relative">
          <div id="traffic-selection" class="center"></div>
          <span class="middle xsmall showing-results absolute">عرض النتائج <?php echo $start; ?> - <?php echo $end; ?> من إجمالى <?php echo $totalPages; ?></span>
        </div> 
           
        <div class="hidden totalPages"><?php if(isset($totalPages)) {echo $totalPages;} ?></div>
        <div class="hidden currentPage"><?php if(isset($page)) {echo $page;} ?></div>
        <?php } } ?>
    </div>
	
<div class="clear"></div>
</div> 
    
